/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.common.builder;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.builder.IXtextBuilderParticipant.IBuildContext;
import org.eclipse.xtext.resource.IResourceDescription.Delta;
 

/**
 * Default implementation of {@link IBuildContextVisitor}.
 *
 * @author Thomas Kistel
 * @version $Revision: 758 $
 *
 */
public class BuildContextVisitor implements IBuildContextVisitor {

    /* (non-Javadoc)
     * @see IBuildContextVisitor#getAffectedFiles(IXtextBuilderParticipant.IBuildContext, java.lang.String)
     */
    @Override
    public List<IFile> getAffectedFiles(IBuildContext context, String... fileExtensions) {
        Assert.isNotNull(context);
        
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        List<Delta> deltas = context.getDeltas();
        List<IFile> affectedFiles = new ArrayList<IFile>();
        for (Delta delta : deltas) {
            URI uri = delta.getUri();
            String platformString = uri.toPlatformString(false);
            if(platformString != null) {
                IFile sourceFile = (IFile)root.findMember(platformString);
                if(hasFileExtension(sourceFile, fileExtensions)) {
                    affectedFiles.add(sourceFile);
                } 
            }
        }
        return affectedFiles;
    }
    
    /**
     * Returns <code>true</code>, if the given <code>file</code> has one of the given <code>fileExtensions</code>.
     */
    private boolean hasFileExtension(IFile file, String... fileExtensions) {
        if(file == null)
            return false;
        // no filtering of file extension, if this field is empty
        if(fileExtensions  == null || fileExtensions.length == 0)
            return true;
        for (String fileExtension : fileExtensions) {
            if(fileExtension.equalsIgnoreCase(file.getFileExtension()))
                return true;
        }
        return false;
    }
}
